const xlsx = require('node-xlsx');
const fs = require('fs');
const path = require('path');

// 配置路径
const _file = `${__dirname}/excel/`
const _output = `${__dirname}/result/`

let total = 200000
let _list = []
const XLSX = require('xlsx');
// 递归读取目录下所有xlsx文件
function readExcelFiles (dir) {
  let results = [];
  let header = []
  const files = fs.readdirSync(dir);

  files.forEach((file) => {
    const fullPath = path.join(dir, file);
    const stat = fs.statSync(fullPath);

    if (stat.isDirectory()) {
      // 递归处理子目录
      results.push(...readExcelFiles(fullPath));
      _list.push(...results.filter(item => {
        return item[18] === "-"
      }))
    } else if (path.extname(file) === '.xlsx') {
      // 读取Excel文件
      try {
        const workSheets = xlsx.parse(fullPath);
        // results.push({
        //   path: fullPath,
        //   data: workSheets
        // });

        if (results.length) {
          results = results.concat(workSheets[0].data.slice(2))
        } else {
          header = workSheets[0].data.slice(1)
          results = results.concat(workSheets[0].data.slice(2));
        }
        _list.push(...results.filter(item => {
          return item[18] === "-"
        }))
        console.log(results.length, workSheets[0].data.length);
        console.log(`成功读取: ${fullPath}`);


        if (results.length >= total) {
          let data = [{ name: 'sheet1', data: [header[0]].concat(results.splice(0, total)) }]
          writeXlsx(data, file)
        }
        console.log(`- 统一社会信用代码为“-”的: ${_list.length} 条`);
        if (_list.length >= total) {
          let temp = [{ name: 'sheet1', data: [header[0]].concat(_list.splice(0, total)) }]
          writeXlsx(temp, file)
        }

      } catch (err) {
        console.error(`读取失败: ${fullPath}`, err);
      }
    }
  });

  return results;
}

function writeXlsx (data, fileName) {

  // console.time();
  // console.log("🍬开始写入", console.log(`${_output}${fileName}.xlsx`));

  // var buffer = xlsx.build(data)
  // fs.writeFile(`${_output}${fileName}.xlsx`, buffer, function (err) {
  //   if (err) {
  //     throw err
  //   }
  //   console.log(`${_output}${fileName}.xlsx`);
  // })
  // console.log("🍬写入完成");
  // console.timeEnd()
  // 确保输出目录存在

  let newlist = data[0].data.slice(1).map((item, index) => {
    let temp = {}
    data[0].data[0].forEach((item2, index2) => { temp[item2] = item[index2] })
    return temp
  })
  console.log(`🍬开始写入：${_output}${fileName}.xlsx`, newlist.length);

  const ws = XLSX.utils.json_to_sheet(newlist);
  const wb = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
  // 生成Excel文件
  XLSX.writeFile(wb, `${_output}${fileName}.xlsx`, {
    compression: true,  // ✔️ 关键压缩选项
    bookSST: true,      // 启用共享字符串表
    bookType: 'xlsx'    // 指定输出格式
  });
  console.log(`👌完成合并：${_output}${fileName}.xlsx`)
  console.timeEnd()
}


(function () {
  let fileName = "end"
  const results = readExcelFiles(_file);
  let data = [{ name: 'sheet1', data: [].concat(results.splice(0, total)) }]
  writeXlsx(data, fileName)

  if (_list.length >= total) {
    let data = [{ name: 'sheet1', data: _list }]
    writeXlsx(data, "end")
  }
})()